<template>
  <div class="me_search">
    <el-dialog
      title="添加部门/人员"
      :visible.sync="SearchUserListData.dialogVisible"
      class="small"
      height="50%"
      :before-close="handleClose"
      :modal-append-to-body="false"
    >
      <div class="box">
        <div class="left">
          <div class="left-top">
            <div class="inputDeep">
              <!--             :select="handleSelect"
                :fetch-suggestions="querySearchAsync" -->
              <el-autocomplete
                v-model="input"
                :fetch-suggestions="querySearchAsync"
                placeholder="搜索名字"
                @select="handleSelect"
              />
            </div>
            <div class="button">
              <!-- closable -->
              <el-tag
                v-for="(tag, index) in tableDataList"
                :key="index"
                :disable-transitions="false"
                class="tag"
                closable
                @close="handleCloses(tag)"
              >
                {{ tag.label }}
              </el-tag>
            </div>
          </div>
          <div class="left-but">
            <el-button type="primary" @click="confirm">确 定</el-button>
            <el-button @click="SearchUserListData.dialogVisible = false"
              >取 消</el-button
            >
          </div>
        </div>
        <div class="right">
          <div class="content">
            <el-table
              ref="tableData"
              height="35vh"
              :data="getdata"
              style="width: 100%"
              :show-header="false"
              :select-on-indeterminate="true"
              :row-key="getRowKeys"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                type="selection"
                width="55"
                :reserve-selection="true"
                :selectable="checkboxT"
              />
              <el-table-column prop="label" />
            </el-table>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer" />
    </el-dialog>
  </div>
</template>

<script>
import '@/styles/search.scss'
import { getsearch } from '@/api/uc/search'
import { searchUserByKeyword } from '@/api/uc/user.js'
import { Verify, arrDifferent } from '@/utils/common'
import { set } from '@/utils/db.js'
export default {
  components: {},
  props: ['SearchUserListData'],
  data() {
    return {
      dialogVisible: false,
      groupUserTree: [],
      roleUserTree: [],
      getdata: [],
      itembox: false,
      isActive: true,
      dataList: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      permissionIds: [],
      input: '',
      breadcrumb: [],
      terrData: [],
      checkedGh: [],
      restaurants: [],
      timeout: null,
      echoData: '',
      teamUserTree: [],
      tableDataList: [],
      searchList: [],
      i: 0,
      newArr: [],
      groupUserList: []
    }
  },
  watch: {
    SelectPersonnelData(val) {},
    getdata(data) {
      this.getItem(data)
    },
    tableDataList(data) {
      arrDifferent(data)
    }
  },
  mounted() {
    this.getsearch()
  },
  methods: {
    dialogVisibles() {
      this.dialogVisible = true
      this.breadcrumb = []
    },

    get() {
      console.log(this.data)
    },
    getMsgFormSon(data) {
      this.dataList = data
    },
    handleClose(done) {
      this.dataList = ''
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 搜索
    querySearchAsync(queryString, cb) {
      // if (Verify(queryString) == true) {
      //   return
      // }
      var restaurants = this.newArr
      var results = queryString
        ? this.createStateFilter(queryString)
        : restaurants
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        cb(results)
      }, 1000 * Math.random())
    },
    createStateFilter(queryString) {
      const data = []
      this.newArr.forEach((item) => {
        if (item.label.indexOf(queryString) === 0 && item.person != false) {
          item.value = item.label
          data.push(item)
        }
      })
      return data
    },
    // 搜索点击列表
    handleSelect(item) {
      this.input = ''
      this.searchList.push(item)
      this.$refs.tableData.toggleRowSelection(item, true)
      this.tableDataList = [...this.searchList, ...this.dataList]
    },
    // 获取数据
    getsearch() {
      getsearch().then((res) => {
        this.getdata = res.data.groupUserTree
      })
    },
    // tag回调
    handleCloses(tag) {
      this.$refs.tableData.toggleRowSelection(tag, false)
      this.$forceUpdate()
    },

    // 点击列表
    click(row) {
      this.$refs.tableData.toggleRowSelection(this.echoData, true)
      if (row.children) {
        this.getdata = row.children
        // this.checkboxT(this.getdata)
        this.breadcrumb.push({ label: row.label, id: row.id })
      }
    },

    checkboxT(row) {
      if (row.id != 1) {
        return true
      } else {
        this.$refs.tableData.toggleRowExpansion(row, true)
        return false
      }

      // return true
    },
    // 点击选择
    handleSelectionChange(sel, row) {
      // console.log(sel, 'eeee')
      const list = sel || []
      // this.dataList = sel || []
      for (let i = 0; i < list.length; i++) {
        if (list[i].children) {
          list[i].children.forEach((item, index) => {
            this.$refs.tableData.toggleRowSelection(item, true)
          })
        }
      }

      this.tableDataList = [...this.searchList, ...this.dataList]
    },

    // 递归树
    getItem(data) {
      data.forEach((item) => {
        this.i++
        item.keys = this.i
        if (item.children) {
          this.getItem(item.children)
        }
      })
    },
    getList(data) {
      data.forEach((item) => {
        this.newArr = [...this.newArr, item]
        if (item.children) {
          this.getList(item.children)
        }
      })
    },
    confirm() {
      this.SearchUserListData.dialogVisible = false
      this.$emit('SearchUserList', this.tableDataList)
    },
    getRowKeys(row) {
      return row.keys
    }
  }
}
</script>

<style scoped>
</style>
